package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrdersettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import com.itheima.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.unit.DataUnit;

import java.util.*;

@Service
@Transactional
@Slf4j
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper,OrderSetting> implements OrdersettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    public void add(List<OrderSetting> list) {

        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                long countByOrderDate = count(Wrappers.<OrderSetting>lambdaQuery().eq(OrderSetting::getOrderDate,orderSetting.getOrderDate()));
                if(countByOrderDate > 0){
                    //已经进行了预约设置，执行更新操作
                    update(Wrappers.<OrderSetting>lambdaUpdate().eq(OrderSetting::getOrderDate,orderSetting.getOrderDate())
                            .set(OrderSetting::getNumber,orderSetting.getNumber()));
                }else{
                    //没有进行预约设置，执行插入操作
                    save(orderSetting);
                }
            }
        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";//2019-6-1
        String end = date + "-31";//2019-6-31

        //调用DAO，根据日期范围查询预约设置数据
        List<OrderSetting> list = list(Wrappers.<OrderSetting>lambdaQuery().between(OrderSetting::getOrderDate,begin,end));
        List<Map> result = new ArrayList<>();
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());//获取日期数字（几号）
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        String date = DateUtils.dateToString(orderSetting.getOrderDate(), "yyyy-MM-dd");
        //根据日期查询是否已经进行了预约设置
        OrderSetting dbOrderSetting = getOne(Wrappers.<OrderSetting>lambdaQuery().eq(OrderSetting::getOrderDate, date));
        if(dbOrderSetting != null){
            //当前日期已经进行了预约设置，需要执行更新操作
            dbOrderSetting.setNumber(orderSetting.getNumber());
            updateById(dbOrderSetting);
        }else{
            //当前日期没有就那些预约设置，需要执行插入操作
            save(orderSetting);
        }
    }
}
